<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 2016/4/7
 * Time: 13:42
 */

namespace App\Http\Controllers\Api\V1;


use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Http\Controllers\Controller;
use App\Interfaces\CommentService;
use App\Interfaces\GoodsService;
use App\Interfaces\CategoryService;
use App\Interfaces\ProductSkuService;
use App\Interfaces\ShopService;
use App\Interfaces\UserCollectionService;
use App\Interfaces\CouponService;
use App\Interfaces\DiscountService;
use App\Models\Goods;
use Config;
use App\Models\ProductAttribute;
use App\Http\Requests\Api\ApiRequest as Request;
use Illuminate\Support\Collection;
use App\Models\ProductSku;
use App\Models\ProductAttributeValue;
use App\Models\CategoryAttribute;
use App\Models\CategoryAttributeValue;
class GoodsController extends Controller{

    /**
     * @api {post} /goods/get/{goods_id} 获取单个商品信息
     * @apiName Goods/get
     * @apiGroup Goods
     * @apiDescription 获取单个商品信息
     * @apiSuccess {Object[]} Object 商品信息
     * @apiSuccess {int}  Object.goods_id   商品ID
     * @apiSuccess {int}  Object.cat_id   分类ID
     * @apiSuccess {string}  Object.goods_sn   商品货号
     * @apiSuccess {string}  Object.goods_name   商品名称
     * @apiSuccess {string}  Object.goods_pet_name   商品副标题
     * @apiSuccess {string}  Object.goods_desc   商品简介
     * @apiSuccess {int}  Object.brand_id   品牌ID
     * @apiSuccess {int}  Object.goods_number   商品库存
     * @apiSuccess {string}  Object.goods_weight   商品重量
     * @apiSuccess {string}  Object.market_price   市场价
     * @apiSuccess {string}  Object.shop_price   商城价格
     * @apiSuccess {string}  Object.keywords   搜索关键词
     * @apiSuccess {string}  Object.goods_content   商品描述
     * @apiSuccess {string}  Object.goods_thumb   商品缩略图
     * @apiSuccess {string}  Object.resource   商品来源
     * @apiSuccess {int}  Object.type   商品类型
     * @apiSuccess {int}  Object.goods_gallery_id   商品相册
     * @apiSuccess {int}  Object.is_on_sale   是否上架
     * @apiSuccess {string}  Object.add_time   添加时间
     * @apiSuccess {int}  Object.sort_order   排序
     * @apiSuccess {int}  Object.is_delete   是否已被删除
     * @apiSuccess {int}  Object.is_home_recommend   是否首页推荐
     * @apiSuccess {int}  Object.is_cat_recommend   是否分类推荐
     * @apiSuccess {int}  Object.shop_id   商品所属商铺id
     * @apiSuccess {int}  Object.sales_num   销售数量
     * @apiSuccess {string}  Object.deleted_at   软删除
     * @apiSuccess {string}  Object.search_keywords   搜索关键词
     * @apiSuccess {int}  Object.warehouse_id   仓库id
     * @apiSuccess {string}  Object.created_at   创建时间
     * @apiSuccess {string}  Object.updated_at   修改时间
     * @apiSuccess {int}  Object.audit_status   核审状态
     * @apiSuccess {string}  Object.mobile_goods_thumb   商品缩略图地址
     * @apiSuccess {Object} Object.discount 商品限时折扣
     * @apiSuccess {Number} Object.discount.discount_id                    限时折扣ID
     * @apiSuccess {String} Object.discount.discount_name                  限时折扣活动名称
     * @apiSuccess {String} Object.discount.discount_label                 标签，显示在商品详情和购买页面
     * @apiSuccess {Number} Object.discount.discount_type                  可用类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {Number} Object.discount.discount_value                 折扣百分比，1-99
     * @apiSuccess {Number} Object.discount.limit_amount                   每位用户限用商品数量，0：不限；大于0表示限用个数
     * @apiSuccess {Number} Object.discount.start_time                     限时折扣开始时间
     * @apiSuccess {Number} Object.discount.end_time                       限时折扣结束时间
     * @apiSuccess {Number} Object.discount.deleted_at                     限时折扣删除时间
     * @apiSuccess {Number} Object.discount.created_at                     限时折扣创建时间
     * @apiSuccess {Number} Object.discount.updated_at                     限时折扣更新时间
     * @apiSuccess {Number} [Object.goods_real_price]                         折扣后的商品单价
     * @apiSuccess {Number} [Object.goods_discount_tip]                       折扣提示，如：7.5折
     * @apiSuccess {string}  Object.goods_bar_code   商品条形码
     * @apiSuccess {string}  Object.tariff_percent   商品关税
     * @apiSuccess {string}  Object.goodsShippingFee   邮费
     * @apiSuccess {Object[]} Object.attribute_value_name 商品属性值名称
     * @apiSuccess {Object} Object.shop_info 商品所属店铺信息
     * @apiSuccess {int} Object.shop_info.id 店铺ID
     * @apiSuccess {string} Object.shop_info.shop_name 店铺名称
     * @apiSuccess {string} Object.shop_info.shop_logo 店铺LOGO
     * @apiSuccess {string} Object.shop_info.shop_desc 店铺描述
     * @apiSuccess {string} Object.shop_info.sort_order 排序
     * @apiSuccess {string} Object.shop_info.is_show 是否展示
     * @apiSuccess {string} Object.shop_info.mobile_shop_logo 店铺LOGO地址
     * @apiSuccess {int} Object.goodsCollectionStatus 商品收藏状态
     * @apiSuccess {Object} Object.coupons 商品优惠券
     * @apiSuccess {Number} Object.coupons.id 优惠券ID
     * @apiSuccess {String} Object.coupons.name 优惠券名称
     * @apiSuccess {Number} Object.coupons.value 优惠券金额
     * @apiSuccess {Number} Object.coupons.amount 优惠券发放数量
     * @apiSuccess {Number} Object.coupons.receive_amount 优惠券已经被领取的数量
     * @apiSuccess {Number} Object.coupons.limit_fir 优惠券使用规则1，0：无限制；大于0：满多少使用
     * @apiSuccess {Number} Object.coupons.type 优惠券可用类型，1：全场通用；2：商品；3：分类；4：店铺；5：品牌
     * @apiSuccess {String} Object.coupons.remarks 优惠券备注说明
     * @apiSuccess {Number} Object.coupons.created_at 优惠券创建时间
     * @apiSuccess {Number} Object.coupons.start_time 优惠券开始时间
     * @apiSuccess {Number} Object.coupons.end_time 优惠券过期时间
     * @apiSuccess {Number} Object.coupons.updated_at 优惠券更新时间
     * @apiSuccess {Number} Object.coupons.deleted_at 优惠券删除时间
     * @apiSuccess {Number} Object.coupons.coupon_type_id 优惠券使用类型主键id
     * @apiSuccess {Number} Object.coupons.coupon_type_use_id 优惠券可使用的类别对应的id（如：goods_id、cat_id、shop_id、brand_id、全场通用则无）
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "goods_id": 1337,
        "cat_id": 434,
        "goods_sn": "BL000125 ",
        "goods_name": "  Sun-Maid 1814g raisins  有机提子干",
        "goods_pet_name": "最好吃的葡萄干",
        "goods_desc": "无污染、纯天然、无籽、肉质多，是最好吃的葡萄干！\r\n世界干果类产品中的一流品牌，老少皆宜，天然健康美味，能真正满足您的健康需要，来自天然，品味纯真",
        "brand_id": 188,
        "goods_number": 17,
        "goods_weight": "0.000",
        "market_price": "129.60",
        "shop_price": "108.00",
        "keywords": "  Sun-Maid ,1814g ,raisins  ,有机提子干,有机,提子干",
        "goods_content": "<p><img alt=\"M1.jpg\" src=\"http://afyg.img-cn-qingdao.aliyuncs.com/424df2248affcb0cbf275c328a274ab3.jpg\" width=\"850\" height=\"578\"><img alt=\"M2.jpg\" src=\"http://afyg.img-cn-qingdao.aliyuncs.com/6e179e2f0b3d4750c0dfa73732ae4bfd.jpg\" width=\"850\" height=\"465\"><img alt=\"m3.jpg\" src=\"http://afyg.img-cn-qingdao.aliyuncs.com/1a6d7abf00b54a664495924857bdd663.jpg\" width=\"850\" height=\"607\"><br></p>",
        "goods_thumb": "74af4847061fbb4dbc8c7160a929b3cc",
        "resource": "美国",
        "type": 0,
        "goods_gallery_id": 0,
        "is_on_sale": 1,
        "add_time": 0,
        "sort_order": 100,
        "is_delete": 0,
        "is_home_recommend": 0,
        "is_cat_recommend": 0,
        "shop_id": 2,
        "sales_num": 0,
        "deleted_at": null,
        "search_keywords": "Sun-Maid,,1814g,,raisins,,,有机提子干,有机,提子干,Sun Maid ,进口美食,饮料、酒水、茶叶,休闲零食,食品,零食,其他/混合坚果,其他,坚果",
        "warehouse_id": 0,
        "created_at": "2016-02-04 16:39:36",
        "updated_at": "2016-04-07 15:16:15",
        "audit_status": 1,
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/74af4847061fbb4dbc8c7160a929b3cc.jpg",
        "discount": {
            "discount_id": 3,
            "discount_name": "9折大酬宾(全场通用)",
            "discount_label": "9折大酬宾哈",
            "discount_type": 1,
            "discount_value": 90,
            "limit_amount": 100,
            "start_time": "2016-07-22 11:00:55",
            "end_time": "2016-07-31 00:00:00",
            "deleted_at": null,
            "created_at": "2016-05-19 10:26:39",
            "updated_at": "2016-07-22 11:00:55"
        },
        "goods_real_price": "0.01",
        "goods_discount_tip": "7.5折",
        "goods_bar_code": null,
        "tariff_percent": 10,
        "goods_shipping_fee": 7
        "attribute_value_name": [
        {
        "id": "36",
        "value": "白色"
        },
        {
        "id": "37",
        "value": "S"
        }
        ],
        "shop_info": {
        "id": 5,
        "shop_name": "阿福易购",
        "shop_logo": "cae871cec556d714de993401961d9619",
        "shop_desc": "阿福易购",
        "sort_order": 0,
        "is_show": 0
        "mobile_shop_logo": "http://dolia.img-cn-hangzhou.aliyuncs.com/cae871cec556d714de993401961d9619.jpg"
        },
        "goodsCollectionStatus": 1,
        "coupons": [
          {
            "id": 1,
            "name": "全场通用优惠券",
            "value": 10,
            "amount": 50,
            "receive_amount": 9,
            "limit_fir": 100,
            "type": 1,
            "remarks": "测试优惠券",
            "created_at": "2016-04-26 19:09:50",
            "start_time": "2016-04-26 00:00:00",
            "end_time": "2016-07-15 00:00:00",
            "updated_at": "2016-07-13 17:50:00",
            "deleted_at": null,
            "coupon_type_id": null,
            "coupon_type_use_id": null
          },
          {
            "id": 2,
            "name": "商品优惠券",
            "value": 20,
            "amount": 100,
            "receive_amount": 16,
            "limit_fir": 200,
            "type": 2,
            "remarks": "20元红包",
            "created_at": "2016-04-29 11:35:01",
            "start_time": "2016-04-29 00:00:00",
            "end_time": "2017-06-30 00:00:00",
            "updated_at": "2016-05-27 14:57:37",
            "deleted_at": null,
            "coupon_type_id": 97,
            "coupon_type_use_id": 967
          },
          {
            "id": 3,
            "name": "分类优惠券",
            "value": 30,
            "amount": 100,
            "receive_amount": 9,
            "limit_fir": 300,
            "type": 3,
            "remarks": "30元优惠券,满300元即可立减30元",
            "created_at": "2016-04-29 11:41:54",
            "start_time": "2016-04-29 00:00:00",
            "end_time": "2017-06-30 00:00:00",
            "updated_at": "2016-05-27 14:57:39",
            "deleted_at": null,
            "coupon_type_id": 81,
            "coupon_type_use_id": 394
          },
          {
            "id": 4,
            "name": "店铺优惠券",
            "value": 40,
            "amount": 100,
            "receive_amount": 9,
            "limit_fir": 400,
            "type": 4,
            "remarks": "40元优惠券，满400元即可立减40元",
            "created_at": "2016-04-29 11:43:25",
            "start_time": "2016-04-29 00:00:00",
            "end_time": "2017-06-30 00:00:00",
            "updated_at": "2016-05-27 14:57:40",
            "deleted_at": null,
            "coupon_type_id": 47,
            "coupon_type_use_id": 5
          },
          {
            "id": 5,
            "name": "品牌优惠券",
            "value": 50,
            "amount": 10,
            "receive_amount": 3,
            "limit_fir": 500,
            "type": 5,
            "remarks": "50元优惠券，满500可用",
            "created_at": "2016-05-19 16:54:06",
            "start_time": "2016-05-19 00:00:00",
            "end_time": "2017-06-30 00:00:00",
            "updated_at": "2016-05-27 14:57:42",
            "deleted_at": null,
            "coupon_type_id": 78,
            "coupon_type_use_id": 99
          }
        ]
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该商品不存在"
    }
     */
    public function get(Request $request,$goods_id,GoodsService $goodsService,CategoryService $categoryService,ProductSkuService $productSkuService,ShopService $shopService,UserCollectionService $collectionService, CouponService $couponService, DiscountService $discountService){

        if(empty($goods_id)){

            throw new ApiInputErrorException('缺少请求参数goods_id');
        }

        $goods = $goodsService->get($goods_id);

        if($goods){

            $goods = $goodsService->get($goods_id)->toArray();
        }else{

            throw new ApiDoneErrorException('该商品不存在');
        }


        $config = config('shipping-price');

        $user = $request->apiUser->get();

        $goodsCollectionStatus = 0;

        if($user){

            $user_id = $request->apiUser->get()->id;

            $userCollection = $collectionService->isInCollection($user_id,"GOODS",$goods_id);

            if($userCollection){

                $goodsCollectionStatus = 1;
            }

            $address = $goodsService->getDefaultAddress($user_id);

            if(!$address){

                $goodsShippingFee = $config['postage'];
            }else{

                $goodsShippingFee = $goodsService->getGoodsShippingFee($goods['type'],$address->address_id,$goods['shop_price']);
            }
        }else{

            $goodsShippingFee = $config['postage'];
        }

        if($goods){
            $shop = $shopService->get($goods['shop_id'])->toArray();

            $productSku = $productSkuService->getByGoodsId($goods_id);

            $product_attribute_arr =  $productSku['product_attribute_arr'];

            $attribute_value_name = new Collection();

            foreach ($product_attribute_arr as $key=>$value) {

                $attribute_value_arr = $value['attribute_value_arr'];

                foreach ($attribute_value_arr as $key=>$value) {

                    if($value['attribute_value_status']==1){

                        $item = ['id'=>$value['attribute_value_id'],'value'=>$value['attribute_value_name']];

                        $attribute_value_name->push($item);
                    }
                }
            }

            $category = $categoryService->get($goods['cat_id']);

            $goods['tariff_percent'] = $category->tariff_percent;

            $goods['goods_shipping_fee'] = $goodsShippingFee;

            $goods['attribute_value_name'] = $attribute_value_name;

            $goods['shop_info'] = $shop;

            $goods['goodsCollectionStatus'] = $goodsCollectionStatus;

            $goods['mobile_goods_content']=$goods['goods_content'];

            $goods['coupons'] = $couponService->getCanReceiveCoupon($goods_id);

            /*$goods['discounts'] = $discountService->getCanUseBiggestDiscountByGoodsId($goods_id);

            if ($goods['discounts']) {

                $goods['goods_real_price'] = sprintf("%.2f",sprintf("%.3f", $goods['shop_price']*$goods['discounts']['discount_value']/100));

                //折扣提示
                $goods['goods_discount_tip'] = '';

                if ($goods['discounts']['discount_value'] % 10 == 0) {
                    $goods['goods_discount_tip'] = $goods['discounts']['discount_value'] / 10;
                }else{

                    $goods['goods_discount_tip'] = intval($goods['discounts']['discount_value'] / 10) . '.' . $goods['discounts']['discount_value'] % 10;;
                }
                $goods['goods_discount_tip'] .= '折';
            }
*/
            return response()->json($goods);

        }else{

            throw new ApiDoneErrorException('该商品不存在');
        }
    }

    /**
     * @api {post} /goods/goods_comment 获取商品评论
     * @apiName Goods/getGoodsComments
     * @apiGroup Goods
     * @apiDescription 获取商品评论
     * @apiParam {int} goods_id 商品ID
     * @apiParam {string} [condition] 根据条件获取商品评论（allComment:全部评论;goodComment:好评,middleComment:中评;badComment:差评）
     * @apiParam {int} [page] 当前页
     * @apiSuccess {Object[]} Object 商品评论信息
     * @apiSuccess {int} Object.allCommentNum 全部评论数
     * @apiSuccess {int} Object.goodCommentNum 好评数
     * @apiSuccess {int} Object.middleCommentNum 中评数
     * @apiSuccess {int} Object.badCommentNum 差评数
     * @apiSuccess {Object[]} Object.comments 评论信息
     * @apiSuccess {int} Object.comments.id 用户评论id
     * @apiSuccess {int} Object.comments.order_goods_id 订单商品表主键id
     * @apiSuccess {int} Object.comments.user_id 用户id
     * @apiSuccess {string} Object.comments.label 标签
     * @apiSuccess {int} Object.comments.star 星级，分1-5级，默认没有评论为5级
     * @apiSuccess {int} Object.comments.platform 评论来源，0：web端；1：手机端
     * @apiSuccess {int} Object.comments.add_time 评论时间
     * @apiSuccess {string} Object.comments.content 评论内容
     * @apiSuccess {string} Object.comments.ip_address 用户评论时IP地址
     * @apiSuccess {int} Object.comments.is_show 是否显示评论，0：不显示；1：显示
     * @apiSuccess {int} Object.comments.type 论评类型：0：用户名；1：匿名
     * @apiSuccess {string} Object.comments.picture 评论图片
     * @apiSuccess {string} Object.comments.mobile_picture 评论图片地址
     * @apiSuccess {int} Object.comments.goods_id 商品ID
     * @apiSuccess {int} Object.comments.phone 手机号
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "allCommentNum": 2,
        "goodCommentNum": 1,
        "middleCommentNum": 0,
        "badCommentNum": 1,
        "comments": {
            "total": 1,
            "per_page": 10,
            "current_page": 1,
            "last_page": 1,
            "next_page_url": null,
            "prev_page_url": null,
            "from": 1,
            "to": 1,
            "data": [
                {
                "id": 103,
                "order_goods_id": 248,
                "user_id": 67,
                "label": null,
                "star": 5,
                "platform": 0,
                "add_time": "2016-04-06 15:38:31",
                "content": "非常棒的一次购物经历，商品配送很快，吃起来更是好吃！强烈推荐！",
                "ip_address": null,
                "is_show": 0,
                "type": 0,
                "picture": "",
                "goods_id": 1673,
                "phone": "18638276188",
                "mobile_picture": null
                }
            ]
        }
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该商品没有评论"
    }
     */

    public function getGoodsComments(Request $request,CommentService $commentService){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        $goods_id = $request->get('goods_id');
        $condition = $request->get('condition')?$request->get('condition'):'allComment';
        $page = $request->get('page');
        //dd($condition);
        if(empty($goods_id)){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }

        $commentInfo = array();
        $comments = $commentService->getComment($goods_id,$condition,$page)->toArray();

        $commentInfo['allCommentNum'] = $commentService->countComment($goods_id);
        $commentInfo['goodCommentNum'] = $commentService->countGoodComment($goods_id);
        $commentInfo['middleCommentNum'] = $commentService->countMiddleComment($goods_id);
        $commentInfo['badCommentNum'] = $commentService->countBadComment($goods_id);
        $commentInfo['comments']=$comments;

        if($commentInfo){
            return response()->json($commentInfo);
        }else{
            throw new ApiDoneErrorException('该商品没有评论');
        }
    }

    /**
     * @api {post} /goods/goods_gallery/{goods_id} 获取商品图集
     * @apiName Goods/getGoodsGallery
     * @apiGroup Goods
     * @apiDescription 获取商品图集
     * @apiSuccess {Object[]} Object 商品图集信息
     * @apiSuccess {int} Object.goods_id 商品ID
     * @apiSuccess {string} Object.pic_1 图1
     * @apiSuccess {string} Object.pic_2 图2
     * @apiSuccess {string} Object.pic_3 图3
     * @apiSuccess {string} Object.pic_4 图4
     * @apiSuccess {string} Object.pic_5 图5
     * @apiSuccess {string} Object.pic_6 图6
     * @apiSuccess {string} Object.pic_7 图7
     * @apiSuccess {string} Object.pic_8 图8
     * @apiSuccess {string} Object.goods_thumb 商品缩略图
     * @apiSuccess {string} Object.pic_1 图1地址
     * @apiSuccess {string} Object.pic_2 图2地址
     * @apiSuccess {string} Object.pic_3 图3地址
     * @apiSuccess {string} Object.pic_4 图4地址
     * @apiSuccess {string} Object.pic_5 图5地址
     * @apiSuccess {string} Object.pic_6 图6地址
     * @apiSuccess {string} Object.pic_7 图7地址
     * @apiSuccess {string} Object.pic_8 图8地址
     * @apiSuccess {string} Object.goods_thumb 商品缩略图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
        "goods_id": 1333,
        "pic_1": "9a1da71b1d73ba10fae89b3557a37a95",
        "pic_2": "0",
        "pic_3": "0",
        "pic_4": "0",
        "pic_5": "0",
        "pic_7": "0",
        "pic_8": "0",
        "pic_6": "0",
        "goods_thumb": "d63e8ea36484eff7df5e52854ab2f44c"，
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/d63e8ea36484eff7df5e52854ab2f44c.jpg",
        "mobile_pic_1": "http://dolia.img-cn-hangzhou.aliyuncs.com/9a1da71b1d73ba10fae89b3557a37a95.jpg",
        "mobile_pic_2": "http://dolia.img-cn-hangzhou.aliyuncs.com/21d1a5a6e6a3aa9fd92f89d8d5632ce3.jpg",
        "mobile_pic_3": "http://dolia.img-cn-hangzhou.aliyuncs.com/97267e4f9ca39e6c3a3422e360cf4bb1.jpg",
        "mobile_pic_4": null,
        "mobile_pic_5": null,
        "mobile_pic_6": null,
        "mobile_pic_7": null,
        "mobile_pic_8": null
        }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该商品没有评论"
    }
     */
    public function getGoodsGallery(Request $request,$goods_id,GoodsService $goodsService){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        if(empty($goods_id)){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        $goodsGallery = $goodsService->getGoodsGallery($goods_id);
        if($goodsGallery){
            return response()->json($goodsGallery);
        }else{
            throw new ApiDoneErrorException('该商品没有相册');
        }
    }

    /**
     * @api {post} /goods/get_cat_goods/{cat_id} 根据分类ID获取商品
     * @apiName Goods/getCatGoods
     * @apiGroup Goods
     * @apiDescription 根据分类ID获取商品
     * @apiParam {int} number 商品数
     * @apiParam {int} [page] 当前页
     * @apiSuccess {Object[]} Object 商品信息
     * @apiSuccess {int}  Object.goods_id   商品ID
     * @apiSuccess {int}  Object.cat_id   分类ID
     * @apiSuccess {string}  Object.goods_sn   商品货号
     * @apiSuccess {string}  Object.goods_name   商品名称
     * @apiSuccess {string}  Object.goods_pet_name   商品副标题
     * @apiSuccess {string}  Object.goods_desc   商品简介
     * @apiSuccess {int}  Object.brand_id   品牌ID
     * @apiSuccess {int}  Object.goods_number   商品库存
     * @apiSuccess {string}  Object.goods_weight   商品重量
     * @apiSuccess {string}  Object.market_price   市场价
     * @apiSuccess {string}  Object.shop_price   商城价格
     * @apiSuccess {string}  Object.keywords   搜索关键词
     * @apiSuccess {string}  Object.goods_content   商品描述
     * @apiSuccess {string}  Object.goods_thumb   商品缩略图
     * @apiSuccess {string}  Object.resource   商品来源
     * @apiSuccess {int}  Object.type   商品类型
     * @apiSuccess {int}  Object.goods_gallery_id   商品相册
     * @apiSuccess {int}  Object.is_on_sale   是否上架
     * @apiSuccess {string}  Object.add_time   添加时间
     * @apiSuccess {int}  Object.sort_order   排序
     * @apiSuccess {int}  Object.is_delete   是否已被删除
     * @apiSuccess {int}  Object.is_home_recommend   是否首页推荐
     * @apiSuccess {int}  Object.is_cat_recommend   是否分类推荐
     * @apiSuccess {int}  Object.shop_id   商品所属商铺id
     * @apiSuccess {int}  Object.sales_num   销售数量
     * @apiSuccess {string}  Object.deleted_at   软删除
     * @apiSuccess {string}  Object.search_keywords   搜索关键词
     * @apiSuccess {int}  Object.warehouse_id   仓库id
     * @apiSuccess {string}  Object.created_at   创建时间
     * @apiSuccess {string}  Object.updated_at   修改时间
     * @apiSuccess {int}  Object.audit_status   核审状态
     * @apiSuccess {string}  Object.goods_bar_code   商品条形码
     * @apiSuccess {string}  Object.mobile_goods_thumb   商品缩略图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
    "total": 306,
    "per_page": "10",
    "current_page": 31,
    "last_page": 31,
    "next_page_url": null,
    "prev_page_url": "http://www.afuyigou.cn/api/goods/get_cat_goods/3?page=30",
    "from": 301,
    "to": 306,
    "data": [
        {
        "goods_id": 1343,
        "cat_id": 398,
        "goods_sn": "33",
        "goods_name": "33",
        "goods_pet_name": "33",
        "goods_desc": "33",
        "brand_id": 74,
        "goods_number": 33,
        "goods_weight": "0.000",
        "market_price": "33.00",
        "shop_price": "33.00",
        "keywords": "33",
        "goods_content": "",
        "goods_thumb": "0d451ef2ed3a0a15950c2cca8a7e4053",
        "resource": "33",
        "type": 1,
        "goods_gallery_id": 0,
        "is_on_sale": 1,
        "add_time": 0,
        "sort_order": 100,
        "is_delete": 0,
        "is_home_recommend": 0,
        "is_cat_recommend": 0,
        "shop_id": 1,
        "sales_num": 0,
        "deleted_at": null,
        "search_keywords": "33,JJBIO,美妆个护,美容彩妆,香水/香氛,香水,香氛,女士香水,香水,女士",
        "warehouse_id": 0,
        "created_at": "2016-02-19 10:10:49",
        "updated_at": "2016-03-16 12:03:42",
        "audit_status": 1,
        "goods_bar_code": null，
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/0d451ef2ed3a0a15950c2cca8a7e4053.jpg"
        }
     ]
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数number"
    }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202 Accepted
    {
    "message": "该分类下没商品"
    }
     */
    public function getCatGoods($cat_id,Request $request, GoodsService $goodsService){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        //dd($request->all());
        $number = $request->get('number');
        $page = $request->get('page');
        if(empty($cat_id)){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        if(empty($number)){
            throw new ApiInputErrorException('缺少请求参数number');
        }
        $goodsGallery = $goodsService->getCatGoods($cat_id,$number,$page);

        if($goodsGallery){
            return response()->json($goodsGallery);
        }else{
            throw new ApiDoneErrorException('该分类下没商品');
        }
    }



    /**
     * @api {post} /goods/shop_goods/{shop_id} 根据店铺ID获取店铺商品
     * @apiName Goods/getGoodsByShop
     * @apiGroup Goods
     * @apiDescription 根据店铺ID获取店铺商品
     * @apiParam {int} [page] 当前页
     * @apiSuccess {Object[]} Object 商品信息
     * @apiSuccess {int} Object.total 总记录
     * @apiSuccess {int} Object.per_page 每页记录数
     * @apiSuccess {int} Object.current_page 当前页数
     * @apiSuccess {int} Object.last_page 最后一页
     * @apiSuccess {string} Object.next_page_url 下一页url地址
     * @apiSuccess {string} Object.prev_page_url 前一页url地址
     * @apiSuccess {Object[]} Object.data 商品信息
     * @apiSuccess {int}  Object.data.goods_id   商品ID
     * @apiSuccess {int}  Object.data.cat_id   分类ID
     * @apiSuccess {string}  Object.data.goods_sn   商品货号
     * @apiSuccess {string}  Object.data.goods_name   商品名称
     * @apiSuccess {string}  Object.data.goods_pet_name   商品副标题
     * @apiSuccess {string}  Object.data.goods_desc   商品简介
     * @apiSuccess {int}  Object.data.brand_id   品牌ID
     * @apiSuccess {int}  Object.data.goods_number   商品库存
     * @apiSuccess {string}  Object.data.goods_weight   商品重量
     * @apiSuccess {string}  Object.data.market_price   市场价
     * @apiSuccess {string}  Object.data.shop_price   商城价格
     * @apiSuccess {string}  Object.data.keywords   搜索关键词
     * @apiSuccess {string}  Object.data.goods_content   商品描述
     * @apiSuccess {string}  Object.data.goods_thumb   商品缩略图
     * @apiSuccess {string}  Object.data.resource   商品来源
     * @apiSuccess {int}  Object.data.type   商品类型
     * @apiSuccess {int}  Object.data.goods_gallery_id   商品相册
     * @apiSuccess {int}  Object.data.is_on_sale   是否上架
     * @apiSuccess {string}  Object.data.add_time   添加时间
     * @apiSuccess {int}  Object.data.sort_order   排序
     * @apiSuccess {int}  Object.data.is_delete   是否已被删除
     * @apiSuccess {int}  Object.data.is_home_recommend   是否首页推荐
     * @apiSuccess {int}  Object.data.is_cat_recommend   是否分类推荐
     * @apiSuccess {int}  Object.data.shop_id   商品所属商铺id
     * @apiSuccess {int}  Object.data.sales_num   销售数量
     * @apiSuccess {string}  Object.data.deleted_at   软删除
     * @apiSuccess {string}  Object.data.search_keywords   搜索关键词
     * @apiSuccess {int}  Object.data.warehouse_id   仓库id
     * @apiSuccess {string}  Object.data.created_at   创建时间
     * @apiSuccess {string}  Object.data.updated_at   修改时间
     * @apiSuccess {int}  Object.data.audit_status   核审状态
     * @apiSuccess {string}  Object.data.goods_bar_code   商品条形码
     * @apiSuccess {string}  Object.data.mobile_goods_thumb   商品缩略图地址
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "total": 1,
        "per_page": 15,
        "current_page": 1,
        "last_page": 1,
        "next_page_url": null,
        "prev_page_url": null,
        "from": 1,
        "to": 1,
        "data": [
        {
        "goods_id": 1339,
        "cat_id": 393,
        "goods_sn": "001",
        "goods_name": "雅漾1",
        "goods_pet_name": "雅漾1",
        "goods_desc": "雅漾",
        "brand_id": 117,
        "goods_number": 5,
        "goods_weight": "0.000",
        "market_price": "22.00",
        "shop_price": "20.00",
        "keywords": "bb霜",
        "goods_content": "",
        "goods_thumb": "0679fcfb50d4b6dde1b8d3ea56ef4ea2",
        "resource": "雅漾",
        "type": 0,
        "goods_gallery_id": 0,
        "is_on_sale": 1,
        "add_time": 0,
        "sort_order": 100,
        "is_delete": 0,
        "is_home_recommend": 0,
        "is_cat_recommend": 0,
        "shop_id": 5,
        "sales_num": 0,
        "deleted_at": null,
        "search_keywords": "bb霜,多宝,美妆个护,美容彩妆,彩妆,彩妆,化妆品,BB霜/CC霜,BB霜,CC霜",
        "warehouse_id": 0,
        "created_at": "2016-02-15 16:01:48",
        "updated_at": "2016-04-07 14:46:26",
        "audit_status": 1,
        "goods_bar_code": ""，
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/0679fcfb50d4b6dde1b8d3ea56ef4ea2.jpg"
        }
        ]
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数shop_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "该店铺下没商品"
    }
     */
    public function getGoodsByShop($shop_id,GoodsService $goodsService,Request $request){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        $page = $request->get('page');
        if(empty($shop_id)){
            throw new ApiInputErrorException('缺少请求参数shop_id');
        }
        $goods = $goodsService->getGoodsByShop($shop_id,$page);
        if($goods){
            return response()->json($goods);
        }else{
            throw new ApiDoneErrorException('该店铺下没商品');
        }
    }

    /**
     * @api {post} /goods/similar_goods/{goods_id} 根据商品ID获取相似商品
     * @apiName Goods/getSimilar
     * @apiGroup Goods
     * @apiDescription 根据商品ID获取相似商品
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    [
        {
        "goods_id": 1328,
        "cat_id": 452,
        "goods_sn": "BL000123",
        "goods_name": "Floradix 有机补铁片 80片",
        "goods_pet_name": "补铁补血 孕妇可用，所有女性必备！！！",
        "goods_desc": "铁元片剂84粒片剂，更易携带.有机补铁，吸收率高达98%，自有平衡机制，当身体铁足够时会自动停止吸收，适合长期使用。",
        "brand_id": 187,
        "goods_number": 10,
        "goods_weight": "0.000",
        "market_price": "153.60",
        "shop_price": "128.00",
        "keywords": "Floradix ,有机补铁片 ,80片,补铁片",
        "goods_content": "",
        "goods_thumb": "b3a19e9b1f827847039c2a5c2587fcc7",
        "resource": "德国",
        "type": 0,
        "goods_gallery_id": 0,
        "is_on_sale": 1,
        "add_time": 0,
        "sort_order": 100,
        "is_delete": 0,
        "is_home_recommend": 0,
        "is_cat_recommend": 0,
        "shop_id": 4,
        "sales_num": 0,
        "deleted_at": null,
        "search_keywords": "Floradix,,有机补铁片,,80片,补铁片,Floradix,营养保健,营养保健,功能保健品,保健,保养,调节免疫/三高,三高,免疫",
        "warehouse_id": 0,
        "created_at": "2016-02-04 16:25:21",
        "updated_at": "2016-04-01 16:59:37",
        "audit_status": 1,
        "mobile_goods_thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/9d059f5bd3356dd3c982271a8fa02b89.jpg"
        }
    ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202 Accepted
    {
    "message": "没有相似商品"
    }
     */
    public function getSimilar($goods_id,GoodsService $goodsService,Request $request){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        if($goods_id==''){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        $similarGoods = $goodsService->getSimilar($goods_id);
        if($similarGoods){
            return response()->json($similarGoods);
        }else{
            throw new ApiDoneErrorException('没有相似商品');
        }
    }

    /**
     * @api {post} /goods/get_product_sku/{goods_id} 获取商品信息及sku
     * @apiName Goods/getProductSku
     * @apiGroup Goods
     * @apiDescription 获取商品信息及sku
     *
     * @apiSuccess {object}  Object   商品信息及产品sku
     * @apiSuccess {object}  Object.goods_info   商品信息
     * @apiSuccess {int}  Object.goods_info.goods_id   商品ID
     * @apiSuccess {string}  Object.goods_info.goods_name   商品名称
     * @apiSuccess {string}  Object.goods_info.thumb   商品图片
     * @apiSuccess {decimal}  Object.goods_info.shop_price   价格
     * @apiSuccess {int}  Object.goods_info.goods_number   库存
     * @apiSuccess {int}  Object.goods_info.goods_real_price   商品折扣后的单价
     * @apiSuccess {object}  Object.product_sku   产品sku
     * @apiSuccess {object}  Object.product_skus   当前商品的sku
     * @apiSuccess {object[]}  Object.product_attribute_arr   属性属性值组合
     * @apiSuccess {int}  Object.cat_id   分类ID
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
    {
        "goods_info": {
            "goods_id": 1232,
            "goods_name": "Power4综合型果汁",
            "thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/fe0cd15c672aecae61f417baa3aea16b.jpg",
            "shop_price": "168.00",
            "goods_number": 0,
            "goods_real_price": "75.00"
        },
        "product_sku": {
            "product_skus": {
                "sku_id": 77,
                "attribute_group": [
                "33",
                "37"
                ],
                "goods_id": 1232,
                "product_id": 2
            },
            "product_attribute_arr": [
                {
                "attribute_id": 48,
                "attribute_name": "颜色",
                "attribute_value_arr": [
                    {
                    "attribute_value_id": "33",
                    "attribute_value_name": "黑色",
                    "attribute_value_status": 1
                    }
                ]
                },
                    {
                    "attribute_id": 50,
                    "attribute_name": "大小",
                    "attribute_value_arr": [
                    {
                    "attribute_value_id": "38",
                    "attribute_value_name": "M",
                    "attribute_value_status": 2
                    },
                    {
                    "attribute_value_id": "37",
                    "attribute_value_name": "S",
                    "attribute_value_status": 1
                    }
                ]
                }
            ]
        }
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     */

    public function getProductSku($goods_id,Request $request,GoodsService $goodsService,ProductSkuService $productSkuService, DiscountService $discountService){
        if($goods_id==''){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        $productSku = $productSkuService->getByGoodsId($goods_id);

        $goods_info = [];
        $goods = $goodsService->get($goods_id);
        $goods_info['goods_id'] = $goods->goods_id;
        $goods_info['goods_name'] = $goods->goods_name;
        $goods_info['thumb'] = $goods->mobile_goods_thumb;
        $goods_info['shop_price'] = $goods->shop_price;
        $goods_info['goods_number'] = $goods->goods_number;
        $goods_info['goods_real_price'] = $goods->goods_real_price;

        /*$discount = $discountService->getCanUseBiggestDiscountByGoodsId($goods_id);

        if ($discount) {

            // $discount = $discount->toArray();

            $goods_info['goods_real_price'] = sprintf("%.2f",sprintf("%.3f", $goods->shop_price * intval($discount->discount_value) / 100));
        }*/

        $data= [
            'goods_info'=>$goods_info,
            'product_sku'=>$productSku
        ];
        return response()->json($data);
    }

    /**
     * @api {post} /goods/get_redirect_goods/{goods_id} 根据选择的属性定位对应的商品
     * @apiName Goods/getRedirectGoods
     * @apiGroup Goods
     * @apiDescription 根据选择的属性定位对应的商品
     * @apiParam {String[]} attributValueArr  属性值ID数组，如：attributValueArr = array(0=>33, 1=>37)
     *
     * @apiSuccess {object}  Object   商品信息及产品sku
     * @apiSuccess {object}  Object.goods_info   商品信息
     * @apiSuccess {int}  Object.goods_info.goods_id   商品ID
     * @apiSuccess {string}  Object.goods_info.goods_name   商品名称
     * @apiSuccess {string}  Object.goods_info.thumb   商品图片
     * @apiSuccess {decimal}  Object.goods_info.shop_price   价格
     * @apiSuccess {int}  Object.goods_info.goods_number   库存
     * @apiSuccess {int}  Object.goods_info.goods_real_price   商品折扣后的单价
     * @apiSuccess {object}  Object.product_sku   产品sku
     * @apiSuccess {object}  Object.product_skus   当前商品的sku
     * @apiSuccess {object[]}  Object.product_attribute_arr   属性属性值组合
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //对应有商品
    {
    "goods_info": {
        "goods_id": 1232,
        "goods_name": "Acai巴西莓果汁",
        "thumb": "http://dolia.img-cn-hangzhou.aliyuncs.com/fe0cd15c672aecae61f417baa3aea16b.jpg",
        "shop_price": "168.00",
        "goods_number": 0,
        "goods_real_price": "75.00"
    },
    "product_sku": {
        "product_skus": {
            "sku_id": 77,
            "attribute_group": [
                "33",
                "37"
            ],
            "goods_id": 1232,
            "product_id": 2
        },
        "product_attribute_arr": [
            {
            "attribute_id": 48,
            "attribute_name": "颜色",
            "attribute_value_arr": [
                {
                "attribute_value_id": "33",
                "attribute_value_name": "黑色",
                "attribute_value_status": 1
                }
            ]
            },
            {
            "attribute_id": 50,
            "attribute_name": "大小",
            "attribute_value_arr": [
                {
                "attribute_value_id": "38",
                "attribute_value_name": "M",
                "attribute_value_status": 2
                },
                {
                "attribute_value_id": "37",
                "attribute_value_name": "S",
                "attribute_value_status": 1
                }
            ]
            }
        ]
        }
    }
     * //对应没有商品
    {
    "goods_info": {
        "goods_id": -1,
        "goods_name": 0,
        "thumb": 0,
        "shop_price": 0,
        "goods_number": 0
    }
    }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数goods_id"
    }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400 Bad Request
    {
    "message": "缺少请求参数attributValueArr"
    }
     */
    public function getRedirectGoods($goods_id,Request $request,GoodsService $goodsService,ProductSkuService $productSkuService, DiscountService $discountService){
//        if(!isset($request->apiSession->session_id)){
//            throw new ApiInputErrorException('header缺少参数session-id');
//        }
        if($goods_id==''){
            throw new ApiInputErrorException('缺少请求参数goods_id');
        }
        $attributValueArr = $request->attributValueArr;
        if(!$attributValueArr){
            throw new ApiInputErrorException('缺少请求参数attributValueArr');
        }
        $redirectGoods = $goodsService->mobileAttributValueRedirect($goods_id,$attributValueArr);
        //dd($attributValueArr);
        if($redirectGoods==-1){
            $goods_info = [];
            $goods_info['goods_id'] =$redirectGoods;
            $goods_info['goods_name'] = "";
            $goods_info['thumb'] = "";
            $goods_info['shop_price'] = "";
            $goods_info['goods_number'] = 0;
            $goods_info['goods_real_price'] = '';

            $data = [
                'goods_info'=>$goods_info
            ];
        }else{
            $goods = Goods::find($redirectGoods);
            $goods_id = $goods->goods_id;

            $productSku = $productSkuService->getByGoodsId($goods_id);

            $goods_info = [];
            $goods = $goodsService->get($goods_id);
            $goods_info['goods_id'] = $goods->goods_id;
            $goods_info['goods_name'] = $goods->goods_name;
            $goods_info['thumb'] = $goods->mobile_goods_thumb;
            $goods_info['shop_price'] = $goods->shop_price;
            $goods_info['goods_number'] = $goods->goods_number;
            $goods_info['goods_real_price'] = $goods->goods_real_price;

            /*$discount = $discountService->getCanUseBiggestDiscountByGoodsId($goods_id);

            if ($discount) {

                // $discount = $discount->toArray();

                $goods_info['goods_real_price'] = sprintf("%.2f",sprintf("%.3f", $goods->shop_price * intval($discount->discount_value) / 100));
            }*/

            $data = [
                'goods_info'=>$goods_info,
                'product_sku'=>$productSku
            ];
        }
        return response()->json($data);

    }



} 